﻿using Coldairarrow.Business.PB;
using Coldairarrow.Entity.DTO;
using Coldairarrow.Entity.PB;
using Coldairarrow.Entity.TD;
using Coldairarrow.IBusiness;
using Coldairarrow.IBusiness.DTO;
using Coldairarrow.Util;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Coldairarrow.Api.Controllers.PB
{
    [Route("/PB/[controller]/[action]")]
    public class PB_OutsourcingBillController : BaseApiController
    {
        #region DI

        public PB_OutsourcingBillController(IPB_OutsourcingBillBusiness pB_OutsourcingBillBus, IOperator op)
        {
            _pB_OutsourcingBillBus = pB_OutsourcingBillBus;
            _Op = op;
        }
        IOperator _Op { get; }
        IPB_OutsourcingBillBusiness _pB_OutsourcingBillBus { get; }

        #endregion

        #region 获取

        [HttpPost]
        public async Task<PageResult<PB_OutsourcingBillViewDTO>> GetDataList(PageInput<PB_OutsourcingBilSelectDTO> input)
        {
            return await _pB_OutsourcingBillBus.GetDataListAsync(input);
        }

        [HttpPost]
        public async Task<PB_ProductionDataListDTO> GetTheData(IdInputDTO input)
        {
            return await _pB_OutsourcingBillBus.GetTheDataAsync(input.id);
        }

        /// <summary>
        /// 打印外包订单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>

        [HttpPost]
        public async Task<PB_OutsourcingBillInfo> GetOutsourcingBillInfo(string id) {
            var res = await _pB_OutsourcingBillBus.GetOutsourcingBillInfo(id);
            return res;
        }

        [HttpPost]
        public async Task<PB_ProductionDataListDTO> GetTheDataInfoPrint(IdInputDTO input)
        {
            return await _pB_OutsourcingBillBus.GetTheDataInfoPrintAsync(input.id);
        }


        /// <summary>
        /// 外包签收明细
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PB_ProductionDataListDTO> GetTheDataInfoSign(IdInputDTO input)
        {

            return await _pB_OutsourcingBillBus.GetTheDataInfoSignAsync(input.id);
        }


        /// <summary>
        /// 外包订单回货签收
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task UpdateData(PB_ProductionDataListDTO data)
        {
            await _pB_OutsourcingBillBus.UpdateDataAsync(data);
        }





        #endregion

        #region 提交

        [HttpPost]
        public async Task SaveData(PB_OutsourcingBillDTO data)
        {
            if (data.Id.IsNullOrEmpty())
            {
                InitEntity(data);

                await _pB_OutsourcingBillBus.AddDataAsync(data);
            }
            else
            {
                await _pB_OutsourcingBillBus.UpdateDataAsync(data);
            }
        }

        [HttpPost]
        public async Task DeleteData(List<string> ids)
        {
            await _pB_OutsourcingBillBus.DeleteDataAsync(ids);

        }

        /// <summary>
        /// 外协审批(+外协签收)
        /// </summary>
        /// <param name="audit"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task Audit(AuditDTO audit)
        {
            audit.AuditUserId = _Op.UserId;
            audit.AuditTime = DateTime.Now;
            if (audit.AuditType == AuditType.Approve)
                await _pB_OutsourcingBillBus.Approve(audit);
            else if (audit.AuditType == AuditType.Signin)
                await _pB_OutsourcingBillBus.Signin(audit); 
            else
                await _pB_OutsourcingBillBus.Reject(audit);
        }


        [HttpPost]
        public async Task RejectData(PB_OutsourcingBillDTO data)
        {
            data.SignforNameId = _Op.UserId;
            data.SignforTime = DateTime.Now;
            await _pB_OutsourcingBillBus.RejectDataAsync(data);

        }



        [HttpPost]
        public async Task Audits(string Id)
        {
            await _pB_OutsourcingBillBus.Start(Id);
        }


        #endregion


        #region PDA 外协出库

        /// <summary>
        /// 1.外协出库,扫描外协单号
        /// </summary>
        /// <param name="outsourqrcode">外协单号</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PB_OutsourcingBillDetail> GetPDADataList(string outsourqrcode)
        {
            return await _pB_OutsourcingBillBus.GetPDADataListAsync(outsourqrcode);
        }

        /// <summary>
        /// 获取备货单对应的包明细
        /// </summary>
        /// <param name="outsourqrcode">外协二维码</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<PB_UnLoadListDataDTO>> GetOutsourcingUpDetailListData(string outsourqrcode)
        {
            return await _pB_OutsourcingBillBus.GetOutsourcingUpDetailListDataAsync(outsourqrcode);
        }



        // 2 扫描货位  调用公共的接口



        /// <summary>
        /// 出库扫描单个物料
        /// </summary>
        /// <param name="packqrcode">物料二维码</param>
        /// <param name="localqrcode">货位二维码</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PB_DispatchDetailPDANumberDTO> GetMaterialData(string packqrcode, string localqrcode)
        {
            return await _pB_OutsourcingBillBus.GetMaterialDataAsync(packqrcode, localqrcode);
        }

        /// <summary>
        /// 判断下架的物料对应的项目是否和外协单上的项目一致
        /// </summary>
        /// <param name="packqrcode">包二维码</param>
        /// <param name="outsourqrcode">外协单</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<bool> GetLocationProjectTruOrFalse(string packqrcode, string outsourqrcode)
        {
            return await _pB_OutsourcingBillBus.GetLocationProjectTruOrFalseAsync(packqrcode, outsourqrcode);
        }

        /// <summary>
        /// 3 扫描物料,并做一条出库日志
        /// </summary>
        /// <param name="data">参数</param>
        /// <returns></returns>
        [HttpPost]
        public async Task UpdatePDAOutsourcingUpData(OutsourcingData data)
        {
            await _pB_OutsourcingBillBus.UpdatePDAOutsourcingUpData(data);
        }

        /// <summary>
        /// 下架出库提交
        /// </summary>
        /// <param name="outsourqrcode">外协单二维码</param>
        /// <returns></returns>
        [HttpPost]
        public async Task SubmitPDAOutsourcingUpData(string outsourqrcode)
        {
            await _pB_OutsourcingBillBus.SubmitPDAOutsourcingUpDataAsync(outsourqrcode);
        }

        /// <summary>
        /// 移除外协备货单已出库的明细
        /// </summary>
        /// <param name="outsourqrcode">外协单二维码</param>
        /// <param name="packqrocde">包二维码</param>
        /// <param name="Nums">数量</param>
        /// <returns></returns>
        [HttpPost]
        public async Task DeletePDAOutsourcingUpAsync(string outsourqrcode, string packqrocde, double Nums)
        {
            await _pB_OutsourcingBillBus.DeletePDAOutsourcingUpDataAsync(outsourqrcode, packqrocde, Nums);
        }


        /// <summary>
        /// 取消外协出库单提交
        /// </summary>
        /// <param name="outsourqrcode">外协二维码</param>
        /// <returns></returns>
        [HttpPost]
        public async Task CancelStockUpAsync(string outsourqrcode)
        {
            await _pB_OutsourcingBillBus.CancelStockUpAsync(outsourqrcode);
        }





        #endregion



        #region 外协流转卡打印

        /// <summary>
        /// 扫描包获取该流转卡
        /// </summary>
        /// <param name="packqrcode">包二维码</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PB_CirculationBillDTO> GetPackDataList(string packqrcode)
        {
            return await _pB_OutsourcingBillBus.GetPackDataListAsync(packqrcode);
        }
        #endregion

        #region  外包单建立

        /// <summary>
        /// 获取物料信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageResult<OutBillMaterialDTO>> GetOutBillMaterialListAsync(PageInput<OutBillMaterialConditionDTO> input)
        {
            return await _pB_OutsourcingBillBus.GetOutBillMaterialListAsync(input);
        }

        #endregion

        #region  外包单

        #region PDA外包单领料

        /// <summary>
        /// PDA出库选择物料信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageResult<OutBillMaterialReceiveDTO>> GetOutBillMaterialReceiveList(PageInput<OutBillMaterialReceiveConditionDTO> input)
        {
            return await _pB_OutsourcingBillBus.GetOutBillMaterialReceiveListAsync(input);
        }

        /// <summary>
        /// PDA物料出库保存
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task SaveOutBillMaterialReceiveData(OutBillMaterialDataDTO data)
        {
            await _pB_OutsourcingBillBus.SaveOutBillMaterialReceiveDataAsync(data);
        }

        /// <summary>
        /// 获取外包单已出库明细
        /// </summary>
        /// <param name="OutBillQrCode">外包单二维码</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<OutsourcingBillUpListDTO>> GetOutsourcingBillUpListAsync(string OutBillQrCode)
        {
            return await _pB_OutsourcingBillBus.GetOutsourcingBillUpListAsync(OutBillQrCode);
        }

        /// <summary>
        /// 移除出库明细
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task DeleteOutsourcingBillUpList(OutsourcingBillUpListDTO data)
        {
            await _pB_OutsourcingBillBus.DeleteOutsourcingBillUpListAsync(data);
        }

        /// <summary>
        /// 提交外包单出库信息
        /// </summary>
        /// <param name="OutBillQrCode">外包单二维码</param>
        /// <returns></returns>
        [HttpPost]
        public async Task SubmitOutsourcingBillUpListAsync(string OutBillQrCode)
        {
            await _pB_OutsourcingBillBus.SubmitOutsourcingBillUpListAsync(OutBillQrCode);
        }

        #endregion

        #region  流转卡生成外包单

        /// <summary>
        /// 获取流转卡物料信息
        /// </summary>
        /// <param name="CirBillQrCode">流转卡二维码</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<OutBillCirculationMaterialDTO> GetOutBillCirculationMaterial(string CirBillQrCode)
        {
            return await _pB_OutsourcingBillBus.GetOutBillCirculationMaterialAsync(CirBillQrCode);
        }

        /// <summary>
        /// 保存流转卡生成外包单
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task SaveOutBillMaterial(CirBillSaveOutBill data)
        {
            await _pB_OutsourcingBillBus.SaveOutBillMaterialAsync(data);
        }

        #endregion

        #region 外包单装车

        /// <summary>
        /// 外包装车扫描外包单
        /// </summary>
        /// <param name="OutBillQrCode">外包单二维码</param>
        /// <param name="BillId">派车单Id</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PB_OutsourcingBill> GetOutsourcingBillData(string OutBillQrCode, string BillId)
        {
            return await _pB_OutsourcingBillBus.GetOutsourcingBillDataAsync(OutBillQrCode, BillId);
        }

        /// <summary>
        /// 获取派车单对应的外包单需要装车的物料
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageResult<TranBillOutMaterialDTO>> GetTranBillOutMaterialList(PageInput<TranBillOutMaterialConditionDTO> input)
        {
            return await _pB_OutsourcingBillBus.GetTranBillOutMaterialListAsync(input);
        }

        /// <summary>
        /// 外包单装车保存
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task SaveTranBillOutMaterial(TranBillOutMaterialDTO data)
        {
            await _pB_OutsourcingBillBus.SaveTranBillOutMaterialAsync(data);
        }

        /// <summary>
        /// 获取外包单装车明细
        /// </summary>
        /// <param name="BillId">派车单Id</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<OutBillTransportBillDetailDTO>> GetOutBillTransportBillDetailAsync(string BillId)
        {
            return await _pB_OutsourcingBillBus.GetOutBillTransportBillDetailAsync(BillId);
        }

        /// <summary>
        /// 提交装车信息
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task SubmitOutBillTransport(PB_TransportBillImgDTO data)
        {
            await _pB_OutsourcingBillBus.SubmitOutBillTransportAsync(data);
        }

        /// <summary>
        /// 移除装车明细
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task DeleteOutBillTransportBillDetail(OutBillTransportBillDetailDTO data)
        {
            await _pB_OutsourcingBillBus.DeleteOutBillTransportBillDetailAsync(data);
        }

        #endregion

        #region 外包单回货

        /// <summary>
        /// 获取外包单位下未完全回货的外包单
        /// </summary>
        /// <param name="OutsourcingCompanyId">外包单位Id</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<PB_OutsourcingBill>> GetOutsourcingBillReturn(string OutsourcingCompanyId)
        {
            return await _pB_OutsourcingBillBus.GetOutsourcingBillReturnAsync(OutsourcingCompanyId);
        }

        /// <summary>
        /// 获取外包回派车单信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<PB_TransportBill>> GetTransportBillOutReturn()
        {
            return await _pB_OutsourcingBillBus.GetTransportBillOutReturnAsync();
        }

        /// <summary>
        /// 获取外包单的明细信息
        /// </summary>
        /// <param name="OutBillId">外包单Id</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<OutMaterialReturnDTO>> GetOutMaterialReturnList(string OutBillId)
        {
            return await _pB_OutsourcingBillBus.GetOutMaterialReturnListAsync(OutBillId);
        }

        /// <summary>
        /// 外包单保存
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task SaveOutMaterialReturn(OutMaterialReturnDataDTO data)
        {
            await _pB_OutsourcingBillBus.SaveOutMaterialReturnAsync(data);
        }

        /// <summary>
        /// 外包回货列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageResult<PB_OutsourcingBillViewDTO>> GetDataReturnList(PageInput<PB_OutsourcingBilSelectDTO> input)
        {
            return await _pB_OutsourcingBillBus.GetDataReturnListAsync(input);
        }

        /// <summary>
        /// 获取外包单的回货明细
        /// </summary>
        /// <param name="OutBillId">外包单Id</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<OutsourcingBillReturnDetailDTO>> GetOutsourcingBillReturnDetail(string OutBillId)
        {
            return await _pB_OutsourcingBillBus.GetOutsourcingBillReturnDetailAsync(OutBillId);
        }

        #endregion

        #endregion

    }
}